package cn.iocoder.yudao.module.contract.controller.admin.basicData;

import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicBankDO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.contract.controller.admin.basicData.vo.*;
import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicBrokerageDO;
import cn.iocoder.yudao.module.contract.service.basicData.BasicBrokerageService;

@Tag(name = "管理后台 - 佣金组")
@RestController
@RequestMapping("/contract/basic-brokerage")
@Validated
public class BasicBrokerageController {

    @Resource
    private BasicBrokerageService basicBrokerageService;

    @PostMapping("/create")
    @Operation(summary = "创建佣金组")
    @PreAuthorize("@ss.hasPermission('contract:basic-brokerage:create')")
    public CommonResult<Long> createBasicBrokerage(@Valid @RequestBody BasicBrokerageSaveReqVO createReqVO) {
        return success(basicBrokerageService.createBasicBrokerage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新佣金组")
    @PreAuthorize("@ss.hasPermission('contract:basic-brokerage:update')")
    public CommonResult<Boolean> updateBasicBrokerage(@Valid @RequestBody BasicBrokerageSaveReqVO updateReqVO) {
        basicBrokerageService.updateBasicBrokerage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除佣金组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('contract:basic-brokerage:delete')")
    public CommonResult<Boolean> deleteBasicBrokerage(@RequestParam("id") Long id) {
        basicBrokerageService.deleteBasicBrokerage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得佣金组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('contract:basic-brokerage:query')")
    public CommonResult<BasicBrokerageRespVO> getBasicBrokerage(@RequestParam("id") Long id) {
        BasicBrokerageDO basicBrokerage = basicBrokerageService.getBasicBrokerage(id);
        return success(BeanUtils.toBean(basicBrokerage, BasicBrokerageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得佣金组分页")
    @PreAuthorize("@ss.hasPermission('contract:basic-brokerage:query')")
    public CommonResult<PageResult<BasicBrokerageRespVO>> getBasicBrokeragePage(@Valid BasicBrokeragePageReqVO pageReqVO) {
        PageResult<BasicBrokerageDO> pageResult = basicBrokerageService.getBasicBrokeragePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BasicBrokerageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出佣金组 Excel")
    @PreAuthorize("@ss.hasPermission('contract:basic-brokerage:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBasicBrokerageExcel(@Valid BasicBrokeragePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BasicBrokerageDO> list = basicBrokerageService.getBasicBrokeragePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "佣金组.xls", "数据", BasicBrokerageRespVO.class,
                        BeanUtils.toBean(list, BasicBrokerageRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得佣金组列表")
    public CommonResult<List<BasicBrokerageDO>> getBasicBrokerageList(@Valid BasicBrokerageDO basicBrokerageDO) {
        List<BasicBrokerageDO> listResult = basicBrokerageService.list(Wrappers.lambdaQuery(basicBrokerageDO));
        return success(listResult);
    }
}